/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package towerdefence.controller;

import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.media.opengl.GL;
import net.phys2d.math.Vector2f;
import net.phys2d.raw.Body;
import net.phys2d.raw.shapes.Box;
import net.phys2d.raw.strategies.QuadSpaceStrategyWithMouse;
import net.phys2d.raw.strategies.QuadSpaceStrategyWithMouse.Space;
import towerdefence.model.Block;
import towerdefence.model.Builder;
import towerdefence.model.Model;
import towerdefence.model.Player;
import towerdefence.model.StepListener;
import towerdefence.model.blocks.StyrofoamBlock;
import towerdefence.view.GLBlockRenderer;
import towerdefence.view.GLBodyRenderer;
import towerdefence.view.GLCore;

/**
 *
 * @author Andreas
 */
public class MasterController implements MouseListener, MouseMotionListener, MouseWheelListener, StepListener, Controller {

    private Model model;
    private GLCore gLCore;
    private Player player;
    private QuadSpaceStrategyWithMouse strategy;
    private Vector2f mouse;
    private boolean active = true;
    private Controller activeController;
    private Body hoveredBody,  selectedBody;

    public MasterController(Model model, GLCore gLCore, Player player, QuadSpaceStrategyWithMouse strategy) {
        this.model = model;
        this.gLCore = gLCore;
        this.player = player;
        this.strategy = strategy;
        this.mouse = strategy.getMouse();
        gLCore.addMouseListener(this);
        gLCore.addMouseMotionListener(this);
        model.addStepListener(this);
        gLCore.addController(this);
        gLCore.addMouseWheelListener(this);
    }

    @Override
    public void mouseClicked(final MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1) {
            if (activeController == null) {
                if (hoveredBody != null) {
                    if (player.getBuilders().contains(hoveredBody)) {
                        selectedBody = hoveredBody;
                        activeController = new BuilderController(gLCore, (Builder) hoveredBody);
                        activeController.enable();
                        gLCore.removeController(this);
                    }
                }
            }
        }

        if (e.getButton() == MouseEvent.BUTTON3) {
            if (activeController != null) {
                activeController.disable();
                activeController = null;
                gLCore.addController(this);
            }
        }


    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        //gLCore.addMo
    }

    @Override
    public void mouseMoved(final MouseEvent e) {
        mouse.x = e.getX();
        mouse.y = e.getY();
    }

    @Override
    public void step() {
        hoveredBody = strategy.getMouseBody();
    }

    @Override
    public void enable() {
        // No effect. Master controller is always enabled.
    }

    @Override
    public void disable() {
        // No effect. Master controller is always enabled.
    }

    @Override
    public void draw(GL gl) {
        if (hoveredBody != null) {
            GLBodyRenderer.drawBody(gl, hoveredBody, Color.YELLOW);
        }
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        //gLCore.getCamera().adjustZoom(e.getWheelRotation() * 0.2f, e.getPoint());
    }
}
